package com.zzq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzq.domain.entity.SmileCheckRecord;
import com.zzq.domain.entity.SmileClient;
import com.zzq.domain.search.CheckRecordSearchParamet;
import com.zzq.enumerate.CheckDayStatusEnum;
import com.zzq.result.SmileResult;
import com.zzq.security.util.UserUtil;
import com.zzq.service.ClientCheckService;
import com.zzq.service.SmileCheckRecordService;
import com.zzq.service.SmilePointRecordService;
import com.zzq.subservice.EarnPointsSubService;
import com.zzq.util.CommonUtils;
import com.zzq.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ClientCheckServiceImpl  implements ClientCheckService {

    @Autowired
    SmileCheckRecordService checkRecordService;

    @Autowired
    SmilePointRecordService pointRecordService;
    @Autowired
    EarnPointsSubService earnPointsSubService;
    @Override
    @Transactional
    public SmileResult check() {
        Date currentDate=new Date();
        SmileClient client = UserUtil.getUser();
        Long clientId = client.getId();
        LambdaQueryWrapper<SmileCheckRecord> wrapper=new LambdaQueryWrapper();
        wrapper.orderByDesc(SmileCheckRecord::getCreatedTime);
        wrapper.eq(SmileCheckRecord::getClientId,clientId);
        List<SmileCheckRecord> checkRecordList = checkRecordService.list(wrapper);

        SmileCheckRecord checkRecordToDay=new SmileCheckRecord();

        checkRecordToDay.setClientNo(client.getClientNo());
        checkRecordToDay.setClientId(clientId);
        checkRecordToDay.setCheckDayStatus(CheckDayStatusEnum.TODAY.getName());
        checkRecordToDay.setCheckYear(CommonUtils.getCurrentYear(currentDate));
        checkRecordToDay.setCheckMonth(CommonUtils.getCurrentMonth(currentDate));
        checkRecordToDay.setCheckDay(CommonUtils.getCurrentDay(currentDate));

        if (ObjectUtils.isEmpty(checkRecordList)){
            //从未签到
            checkRecordToDay.setCheckAllNum(1);
            checkRecordToDay.setCheckSustainedNum(1);
            checkRecordService.save(checkRecordToDay);
            earnPointsSubService.cheakPoints(clientId, client.getClientNo());
            return SmileResult.sucess("签到成功",1);
        }
        else {
            SmileCheckRecord smileCheckRecord = checkRecordList.get(0);
            String checkDayStatus = smileCheckRecord.getCheckDayStatus();
            //今日已签到
            if (CheckDayStatusEnum.TODAY.getName().equals(checkDayStatus)
                    &&
                    CommonUtils.isSameDay(smileCheckRecord.getCreatedTime(),new Date())) {

                return SmileResult.waring("今日已签到",1);
            }else {
                //今日未签到
                Date lastCreatedTime = smileCheckRecord.getCreatedTime();
                boolean withinDays = CommonUtils.isWithinDays(lastCreatedTime, new Date(), 1);
                Integer checkSustainedNum=1;
                if (withinDays){
                    checkSustainedNum =( smileCheckRecord.getCheckSustainedNum()+1);
                }
                Integer getCheckAllNum = smileCheckRecord.getCheckAllNum()+1;
                checkRecordToDay.setCheckAllNum(getCheckAllNum);
                checkRecordToDay.setCheckSustainedNum(checkSustainedNum);
                checkRecordService.save(checkRecordToDay);

                //获取签到积分
                earnPointsSubService.cheakPoints(clientId, client.getClientNo());
                return SmileResult.sucess("签到成功",checkSustainedNum);
            }

        }

    }

    @Override
    public SmileResult checkStatus() {
        SmileClient client = UserUtil.getUser();
        Long clientId = client.getId();
        LambdaQueryWrapper<SmileCheckRecord> wrapper=new LambdaQueryWrapper();
        wrapper.eq(SmileCheckRecord::getCheckDayStatus,CheckDayStatusEnum.TODAY);
        wrapper.eq(SmileCheckRecord::getClientId,clientId);
        List<SmileCheckRecord> checkRecordList = checkRecordService.list(wrapper);
        if (!ObjectUtils.isEmpty(checkRecordList)) {
            SmileCheckRecord smileCheckRecord = checkRecordList.get(0);
                return SmileResult.sucess("今日已签到",true);

        }
        return SmileResult.sucess("今日未签到",false);
    }

    @Override
    public SmileResult getCheckRecordList(CheckRecordSearchParamet searchParamet) {
        Long clientId = searchParamet.getClientId();
        LambdaQueryWrapper<SmileCheckRecord> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(SmileCheckRecord::getClientNo,searchParamet.getClientNo());
        queryWrapper.eq(SmileCheckRecord::getCheckYear,searchParamet.getCheckYear());
        queryWrapper.eq(SmileCheckRecord::getCheckMonth,searchParamet.getCheckMonth());
        List<SmileCheckRecord> list = checkRecordService.list(queryWrapper);
        return SmileResult.sucess("获取成功",list);
    }

}
